<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" ref="queryForm" label-width="88px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item :label="$t('inventoryManagement.materialNo')" prop="materialCode" :span="8">
                            <el-input v-model="queryParams.materialCode" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.materialNo')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!-- 物料名称-->
                        <el-form-item :label="$t('inventoryManagement.materialName')" prop="materialName">
                            <el-input v-model="queryParams.materialName" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.materialName')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!-- 供应商编号-->
                        <el-form-item :label="$t('inventoryManagement.supplierNo')" prop="supplierCode">
                            <el-input v-model="queryParams.supplierCode" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.supplierNo')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!-- SPA批次 -->
                        <el-form-item :label="$t('inventoryManagement.batch')" prop="lot">
                            <el-select v-model="queryParams.lot" filterable size="small" @change="lotChange" class="line_select" reserve-keyword :placeholder="$t('public.inputPl')">
                                <el-option v-for="(item,index) in lotOptions" :key="index" :label="item" :value="item">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!-- OA审批单号 -->
                        <el-form-item :label="$t('defectIncoming.oaApprovalNo')" prop="ncrProcessId">
                            <el-select v-model="queryParams.ncrProcessId" filterable @change="ncrProcessIdChange" size="small" class="line_select" reserve-keyword :placeholder="$t('public.inputPl')">
                                <el-option v-for="(item,index) in oaOptions" :key="index" :label="item" :value="item">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!-- 箱号-->
                        <el-form-item :label="$t('defectIncoming.caseNo')" prop="handlinguint">
                            <el-input v-model="queryParams.handlinguint" :placeholder="$t('public.placeholder',{placeholder: $t('defectIncoming.caseNo')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!-- 决策 -->
                        <el-form-item :label="$t('defectIncoming.decision')" prop="decision">
                            <el-select v-model="queryParams.decision" :placeholder="$t('public.selectPl')+ $t('defectIncoming.decision')" size="small">
                                <el-option :label="$t('Toberead.all')" value="" />
                                <el-option v-for="dict in dict.type.NYIQC_DECISION" :key="dict.label" :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div style="display:flex;justify-content: center;">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.query')}}</el-button>
                <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
            </div>
        </div>
        <div class="app-container">
            <div class="container-header">
                <div class="header-left">
                    {{$t('defectIncoming.selectUse')}}
                </div>
            </div>
            <div class="container-table">
                <el-row type="flex" justify="space-between" align="middle">
                    <div v-if="isFlagBit == '1'">
                        <el-button class="query" v-hasPermi="['ny-iqc:nlenr:updateDecisionJieshu']" icon="el-icon-my-accept" size="mini" @click="handleAccept()">{{$t('defectIncoming.accept')}}</el-button>
                        <el-button size="mini" v-hasPermi="['ny-iqc:nlenr:updateDecisionTuiHuo']" class="resectBtn" icon="el-icon-my-return" @click="handleReturn()">{{$t('defectIncoming.return')}}</el-button>
                        <el-button size="mini" v-hasPermi="['ny-iqc:nlenr:getCompleteDecision']" class="decision" icon="el-icon-my-accept" @click="handleCompleteDecision">{{$t('defectIncoming.completeDecision')}}</el-button>
                    </div>
                    <div class="totalNum">
                        <span>{{$t('defectIncoming.currentBatch')}} </span>
                        <span>{{$t('defectIncoming.undecidedBatch1')}}：<span class="c1">{{nlenrTotal.weijuece}} </span>{{$t('defectIncoming.nlenr')}}</span>｜
                        <span>{{$t('defectIncoming.undecidedBatch2')}}：<span class="c2">{{nlenrTotal.tuihuojuece}} </span>{{$t('defectIncoming.nlenr')}}</span> ｜
                        <span>{{$t('defectIncoming.undecidedBatch3')}}：<span class="c3">{{nlenrTotal.jieshoujuece}} </span>{{$t('defectIncoming.nlenr')}}</span>
                    </div>
                </el-row>
                <el-table border stripe v-loading="loading" :data="typeList" @selection-change="selectChange" max-height="500" style="margin-top:10px">
                    <el-table-column type="selection" width="55" />
                    <el-table-column :label="$t('inventoryManagement.index')" type="index" />
                    <el-table-column :label="$t('inventoryManagement.batch')" prop="lot" show-overflow-tooltip />
                    <el-table-column :label="$t('defectIncoming.caseNo')" prop="handlinguint" show-overflow-tooltip />
                    <el-table-column :label="$t('defectIncoming.decisionRes')" prop="decision" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span class="labelN" v-if="scope.row.decision == 0">{{$t('defectIncoming.unAccept')}}</span>
                            <span class="labelY" v-if="scope.row.decision == 1">{{$t('defectIncoming.accept')}}</span>
                            <span class="labelColor0" v-if="scope.row.decision == 2">{{$t('defectIncoming.return')}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('defectIncoming.editTime')" prop="editTime" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('defectIncoming.decisionMan')" prop="decisionBy" :show-overflow-tooltip="true" />

                    <el-table-column label="【SAP】结果" align="left" prop="sapResult" minWidth="100" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('defectIncoming.decisionOpera')" v-if="isFlagBit == '1'" fixed="right" align="center" width="160">
                        <template slot-scope="scope">
                            <div class="action-wrap">
                                <table-body-btn type="gwSubmit" v-hasPermi="['ny-iqc:nlenr:updateDecisionJieshu']" v-if="scope.row.decision == 0 || scope.row.decision == 2" :name="$t('defectIncoming.accept')" @click.native="handleAccept(scope.row)" />
                                <table-body-btn type="gwClose" v-hasPermi="['ny-iqc:nlenr:updateDecisionTuiHuo']" v-if="scope.row.decision == 0 || scope.row.decision == 1" :name="$t('defectIncoming.return')" @click.native="handleReturn(scope.row)" />
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total>0" :total="total" :pageSizes="[50,100]" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
            </div>

        </div>
    </list-layout>
</template>

<script>
import {
  getCompleteDecision,
  getCompleteDecisionHint,
  getHuiZong,
  getLot,
  getNcrprocessId,
  getNlenrList,
  updateDecisionJieshu,
  updateDecisionTuiHuo,
} from '@/api/inspectionManagement/inspectionManagement'
//统计管理-BOM物料详情
export default {
    name: 'selectUses',
    dicts: ['NYIQC_DECISION'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                lot: '',
                supplierCode: '',
                materialName: '',
                materialCode: '',
                ncrProcessId: '',
                nlenr: '',
                decision: '',
            },
            nlenrTotal: {
                weijuece: 0,
                zongcout: 0,
                tuihuojuece: 0,
                jieshoujuece: 0,
            },
            lotOptions: [],
            oaOptions: [],
            isFlagBit: '1',
            activeName: 'all',
        }
    },
    created() {
        let query = this.$route.query
        console.log(query)
        if (JSON.stringify(query) != '{}') {
            this.queryParams = Object.assign({}, this.queryParams, { ...query })
        }
        this.getSPAList()
    },
    methods: {
        /** 查询字典类型列表 */
        getList() {
            this.loading = true
            let params = { ...this.queryParams }
            delete params.materialName
            delete params.materialCode
            delete params.supplierCode
            delete params.ncrProcessId
            getNlenrList(params).then((response) => {
                if (response.code == 200 && response.rows) {
                    if (response.rows.length) {
                        this.isFlagBit = response.rows[0].flagBit
                    }
                    this.typeList = response.rows
                    this.total = response.total
                    this.loading = false
                }
            })
        },
        // 获取spa
        getSPAList() {
            console.log(this.queryParams, 'this.queryParams')
            getLot({
                lot: this.queryParams.lot,
                materialCode: this.queryParams.materialCode,
                materialName: this.queryParams.materialName,
                supplierCode: this.queryParams.supplierCode,
            }).then((res) => {
                this.lotOptions = res.data
                if (res.data.length) {
                    this.queryParams.lot = res.data[0]
                }
                this.getOAList(this.queryParams.lot)
            })
        },
        // 获取oa
        getOAList(lot) {
            getNcrprocessId({
                lot: lot,
            }).then((res) => {
                this.oaOptions = res.data
                this.queryParams.ncrProcessId = res.data[0]
                this.getNlenrTotal(lot, this.queryParams.inspectionLotMark)
                this.getList()
            })
        },
        lotChange(e) {
            getNcrprocessId({
                lot: e,
            }).then((res) => {
                this.oaOptions = res.data
                if (res.data.length) {
                    this.queryParams.ncrProcessId = res.data[0]
                } else {
                    this.queryParams.ncrProcessId = ''
                }
                this.getNlenrTotal(e, this.queryParams.inspectionLotMark)
            })
        },
        ncrProcessIdChange(e) {
            this.getNlenrTotal(this.queryParams.lot, e)
        },
        // 汇总
        getNlenrTotal(lot, inspectionLotMark) {
            getHuiZong({
                lot,
                inspectionLotMark,
            }).then((res) => {
                if (res.code === 200) {
                    this.nlenrTotal = res.data
                }
            })
        },
        selectChange(e) {
            this.ids = e.map((item) => item.id)
            console.log(e)
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.getSPAList()
        },
        handleAccept(row) {
            let list
            if (row) {
                list = row.id
            } else {
                if (this.ids.length === 0) {
                    this.$modal.msgWarning(this.$t('formErrMsg.selectNlenr'))
                    return
                } else {
                    list = this.ids.join(',')
                }
            }
            updateDecisionJieshu(list).then((res) => {
                if (res.code === 200) {
                    this.$modal.msgSuccess(res.msg)
                    this.getNlenrTotal(
                        this.queryParams.lot,
                        this.queryParams.ncrProcessId
                    )
                    this.getList()
                }
            })
        },
        handleReturn(row) {
            let list
            if (row) {
                list = row.id
            } else {
                if (this.ids.length === 0) {
                    this.$modal.msgWarning(this.$t('formErrMsg.selectNlenr'))
                    return
                }
                list = this.ids.join(',')
            }
            updateDecisionTuiHuo(list).then((res) => {
                if (res.code === 200) {
                    this.$modal.msgSuccess(res.msg)
                    this.getNlenrTotal(
                        this.queryParams.lot,
                        this.queryParams.ncrProcessId
                    )
                    this.getList()
                }
            })
        },
        handleCompleteDecision() {
            if (this.typeList.length) {
                getCompleteDecisionHint({
                    id: this.queryParams.id,
                }).then((res) => {
                    let { handlinguint, tuihuojuece, jieshoujuece } = res.data
                    if (res.code === 200) {
                        let html = `<div>${this.$t(
                            'defectIncoming.thisBatch'
                        )} <span style="color:#eb5648">${tuihuojuece}</span> ${this.$t(
                            'defectIncoming.nlenr'
                        )}，${this.$t(
                            'defectIncoming.accept'
                        )} <span style="color:#20b09f">${jieshoujuece}</span> ${this.$t(
                            'defectIncoming.nlenr'
                        )}，${this.$t(
                            'defectIncoming.caseNoFollows'
                        )}：</div><div>${handlinguint.join(',')}</div>`
                        this.$confirm(
                            html,
                            `${this.$t('defectIncoming.confirmOpera')}？`,
                            {
                                dangerouslyUseHTMLString: true,
                                confirmButtonText: this.$t('modal.yes'),
                                cancelButtonText: this.$t('modal.no'),
                                type: 'warning',
                            }
                        )
                            .then(() => {
                                getCompleteDecision({
                                    id: this.queryParams.id,
                                }).then((res) => {
                                    if (res.code === 200) {
                                        this.$modal.msgSuccess(res.msg)
                                        this.getNlenrTotal(
                                            this.queryParams.lot,
                                            this.queryParams.ncrProcessId
                                        )
                                        this.getList()
                                    }
                                })
                            })
                            .catch(() => {})
                    } else {
                        this.$modal.msgWarning(res.msg)
                    }
                })
            } else {
                this.$modal.msgWarning(this.$t('defectIncoming.notDeciCont'))
            }
        },
        handleClick() {
            if (this.activeName !== 'all') {
                let io = this.dict.type.NYIQC_DECISION.find((item) => {
                    return item.value == this.activeName
                })
                this.queryParams.decision = io.value
                this.getList()
            } else {
                this.queryParams.decision = ''
                this.getList()
            }
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    float: left;
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    line-height: 40px;
    display: flex;
    justify-content: center;
    border-bottom: 2px solid #0b7be3;
}
.header-right {
    float: right;
}
.container-header {
    float: left;
    width: 100%;
    border-bottom: 1px solid #ededf2;
}
.container-table {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 100%;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}

.totalNum {
    text-align: right;
    font-size: 12px;
    color: #999999;
    span {
        // margin-left: 10px;
    }
}

::v-deep .el-icon-my-accept {
    background: url('~@/assets/icons/table/gou.png') no-repeat;
    font-size: 12px;
    background-size: cover;
}

::v-deep .el-icon-my-accept:before {
    content: '替';
    font-size: 12px;
    visibility: hidden;
}

::v-deep .el-icon-my-return {
    background: url('~@/assets/icons/table/cha.png') no-repeat;
    font-size: 12px;
    background-size: cover;
}

::v-deep .el-icon-my-return:before {
    content: '替';
    font-size: 12px;
    visibility: hidden;
}
.decision {
    background: #0b7be3;
    border-color: #0b7be3;
    color: #fff;
}
.c1 {
    color: #000;
}
.c2 {
    color: #eb5648;
}
.c3 {
    color: #20b09f;
}
</style>
